#-*-coding:utf-8-*-
'''
Created on 2009/6/15

@author: Hung-Hsi Chen
'''

import urllib
import urllib2
import string

if __name__ == '__main__':
    
    for year in xrange(1990, 2010):
        for month in xrange(1, 13):
            formatedMon = string.zfill(month, 2)
            url = 'http://www.twse.com.tw/ch/trading/exchange/FMTQIK/genpage/Report%s%s/%s%s_F3_1_2.php?STK_NO=&myear=%s&mmon=%s'% \
            (year, formatedMon, year, formatedMon, year, formatedMon)
            try:
                response = urllib2.urlopen(url)
                content = response.read()
                filename = "C:/stock_tmp/twse_%s%s.htm"%( year, formatedMon)
                file = open(filename, 'w')
                file.write(content)
                file.close()
                print "%s download complete"%filename
            except urllib2.HTTPError:
                print "%s not found!!"%url
                continue 